-
-
Notifications
You must be signed in to change notification settings - Fork 781
Tests and example for how to use relationship alias. #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
📝 Docs preview for commit 9606fac at: https://2236fb7a.sqlmodel.pages.dev |
|
📝 Docs preview for commit daf8e90 at: https://537fb381.sqlmodel.pages.dev |
|
📝 Docs preview for commit 82e8655 at: https://4ab2c1a2.sqlmodel.pages.dev |
|
📝 Docs preview for commit 046ef20 at: https://10831b49.sqlmodel.pages.dev |
When one object has more than one relationship to the same foreign object, you need to use `aliased` to differentiate between the relationships.
046ef20 to
bd2d61a
Compare
|
📝 Docs preview for commit bd2d61a at: https://84f23520.sqlmodel.pages.dev |
|
📝 Docs preview for commit d5f78f2 at: https://735afb3a.sqlmodel.pages.dev Modified Pages |
YuriiMotov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cycledriver, thanks for working on this!
I really like this!
Are you ready to work a little more on this PR to polish it?
| ```Python hl_lines="11 15" | ||
| # Code above omitted 👆 | ||
|
|
||
| {!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py[ln:13-26]!} | ||
|
|
||
| # Code below omitted 👇 | ||
| ``` | ||
|
|
||
| /// details | 👀 Full file preview | ||
|
|
||
| ```Python | ||
| {!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py!} | ||
| ``` | ||
|
|
||
| /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ```Python hl_lines="11 15" | |
| # Code above omitted 👆 | |
| {!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py[ln:13-26]!} | |
| # Code below omitted 👇 | |
| ``` | |
| /// details | 👀 Full file preview | |
| ```Python | |
| {!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py!} | |
| ``` | |
| /// | |
| {* ./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001_py310.py ln[13:26] hl[11,15] *} |
The format of includes has been changed and now it's much simpler
| ``` | ||
|
|
||
| /// | ||
| ## Searching for Heros |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding a "Get hero's teams" section and show how easy it is to access Hero.winter_team and Hero.summer_team?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this what you were thinking about? https://github.com/fastapi/sqlmodel/pull/815/files#diff-80f9757360022c326b802fa59508223afc314174233caa81da4aa1091c56a643R90
This comment was marked as duplicate.
This comment was marked as duplicate.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant something like
hero = session.get(Hero, hero_id)
print(f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}")
| ) | ||
| summer_team_id: int | None = Field(default=None, foreign_key="team.id") | ||
| summer_team: Team | None = Relationship( | ||
| sa_relationship_kwargs={"primaryjoin": "Hero.summer_team_id == Team.id"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more question - why did you choose to specify primaryjoin and not foreign_keys here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember. Our code was probably using "primaryjoin" for other things already. I'll give both a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like primaryjoin and foreign_keys work the same way in this case. I dug into the sqlalchemy docs a bit more and it looks like foreign_keys is the recommended way to to this: https://docs.sqlalchemy.org/en/20/orm/join_conditions.html#handling-multiple-join-paths.
|
📝 Docs preview for commit 870dc4c at: https://fd33507a.sqlmodel.pages.dev Modified Pages |
Co-authored-by: Motov Yurii <[email protected]>
|
📝 Docs preview for commit fe0187c at: https://1ee3be21.sqlmodel.pages.dev Modified Pages |
…w multiple solutions
|
📝 Docs preview for commit 10b79b9 at: https://92e71f5f.sqlmodel.pages.dev Modified Pages |
…etreiveal of foreign info example
|
📝 Docs preview for commit bf737bf at: https://72e69b95.sqlmodel.pages.dev Modified Pages |
|
Did some more research and found two ways to do this. Added a bit more info about both ways and why you might choose one over the other. |
|
📝 Docs preview for commit f419d74 at: https://0c78ac11.sqlmodel.pages.dev Modified Pages |
|
📝 Docs preview for commit dfdaf23 at: https://3fe6f59d.sqlmodel.pages.dev Modified Pages |
|
📝 Docs preview for commit d514df2 at: https://b0d9ef89.sqlmodel.pages.dev Modified Pages |
|
📝 Docs preview for commit 1f80fcb at: https://1e106eea.sqlmodel.pages.dev Modified Pages |
|
📝 Docs preview for commit a6e1492 at: https://64938733.sqlmodel.pages.dev Modified Pages |
| ```Python hl_lines="11 15" | ||
| # Code above omitted 👆 | ||
|
|
||
| {!./docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py[ln:13-26]!} | ||
|
|
||
| # Code below omitted 👇 | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use new code include format:
{* ./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001_py310.py ln[13:26] hl[11,15] *}
| ``` | ||
|
|
||
| /// | ||
| ## Searching for Heros |
This comment was marked as duplicate.
This comment was marked as duplicate.
Sorry, something went wrong.
|
As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR. |
|
@cycledriver, do you have plans to continue working on this? |
Needed to figure out how to use an alias to refer to the same table with 2 different attributes. Thought I would share share my learning by adding to the tutorial.